package com.zhr.tea.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhr.tea.R;
import com.zhr.tea.entity.CarGoodsBean;
import com.zhr.tea.view.adapter.selector.SectionedRecyclerViewAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * .::::.
 * .::::::::.
 * :::::::::::
 * ..:::::::::::'
 * '::::::::::::'
 * .::::::::::
 * '::::::::::::::..
 * ..::::::::::::.
 * ``::::::::::::::::
 * ::::``:::::::::'        .:::.
 * ::::'   ':::::'       .::::::::.
 * .::::'      ::::     .:::::::'::::.
 * .:::'       :::::  .:::::::::' ':::::.
 * .::'        :::::.:::::::::'      ':::::.
 * .::'         ::::::::::::::'         ``::::.
 * ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 * '.:::::'                    ':'````..
 * Created by 毛懿 on 2016/11/3 09:05.
 * 邮箱：1334964028@qq.com
 */

public class ShopCarAdapter extends SectionedRecyclerViewAdapter<ShopCarAdapter.HeadViewHolder,
        ShopCarAdapter.ItemViewHolder, RecyclerView.ViewHolder> {

    private Context context;
    private List<CarGoodsBean.ContentBean> goods;
    private PriceChange priceChange;

    public ShopCarAdapter(Context context, List<CarGoodsBean.ContentBean> goods, PriceChange priceChange) {
        this.context = context;
        this.goods = goods;
        this.priceChange = priceChange;
    }

    public void setOnPriceChange() {
        priceChange.change();
    }

    @Override
    protected int getSectionCount() {
        return goods.size();
    }

    @Override
    protected int getItemCountForSection(int section) {
        return goods.get(section).getGoodsList().size();
    }

    @Override
    protected boolean hasFooterInSection(int section) {
        return false;
    }

    @Override
    protected HeadViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
        return new HeadViewHolder(LayoutInflater.from(context).inflate(R.layout.item_car_goods_group, parent, false));
    }

    @Override
    protected RecyclerView.ViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    protected ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(context).inflate(R.layout.item_car_goods_item, parent, false));
    }

    @Override
    protected void onBindSectionHeaderViewHolder(final HeadViewHolder holder, final int section) {
        Glide.with(context).load(goods.get(section).getShopLogoUrl()).placeholder(R.mipmap.abc_ic_menu_copy_mtrl_am_alpha).into(holder.ivCarGroup);
        holder.tvCarGroupTitle.setText(goods.get(section).getShopName());
        holder.ivCarGroupXuanzhong.setChecked(goods.get(section).isCheckedG());
        holder.tvCarGroupTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击品牌跳转
//                Toast.makeText(context, "", Toast.LENGTH_LONG).show();
            }
        });
        holder.ivCarGroupXuanzhong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goods.get(section).setCheckedG(holder.ivCarGroupXuanzhong.isChecked());
                for (int i = 0; i < goods.get(section).getGoodsList().size(); i++) {
                    goods.get(section).getGoodsList().get(i).setCheckedI(holder.ivCarGroupXuanzhong.isChecked());
                }
                priceChange.change();
                notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void onBindSectionFooterViewHolder(RecyclerView.ViewHolder holder, int section) {

    }

    @Override
    protected void onBindItemViewHolder(final ItemViewHolder holder, final int section, final int position) {
        Glide.with(context).load(goods.get(section).getGoodsList().get(position).getGoodsImgUrl()).placeholder(R.mipmap.ic_launcher).into(holder.ivCarGoods);
        holder.tvCarGroupTitle.setText(goods.get(section).getGoodsList().get(position).getGoodsName() + "  " + goods.get(section).getGoodsList().get(position).getGoodsDes());
        holder.tvCarGroutPrice.setText("￥" + goods.get(section).getGoodsList().get(position).getGoodsPrice());
        holder.tvCarGroupCount.setText(goods.get(section).getGoodsList().get(position).getBuyCount());
        holder.ivCarItemXuanzhong.setChecked(goods.get(section).getGoodsList().get(position).isCheckedI());
        holder.ivCarGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击商品跳转到商品信息页面
            }
        });
        holder.ivCarJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Integer count = Integer.valueOf(goods.get(section).getGoodsList().get(position).getBuyCount());
                count++;
                goods.get(section).getGoodsList().get(position).setBuyCount(String.valueOf(count));
                priceChange.change();
                notifyDataSetChanged();
            }
        });
        holder.ivCarJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Integer count = Integer.valueOf(goods.get(section).getGoodsList().get(position).getBuyCount());
                if (count != 0) {
                    count--;
                }
                goods.get(section).getGoodsList().get(position).setBuyCount(String.valueOf(count));
                priceChange.change();
                notifyDataSetChanged();
            }
        });
        holder.ivCarItemXuanzhong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goods.get(section).getGoodsList().get(position).setCheckedI(holder.ivCarItemXuanzhong.isChecked());
                if (!holder.ivCarItemXuanzhong.isChecked()) {
                    goods.get(section).setCheckedG(false);
                }
                priceChange.change();
                notifyDataSetChanged();
            }
        });
    }

    public class HeadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_car_group_xuanzhong)
        CheckBox ivCarGroupXuanzhong;
        @BindView(R.id.iv_car_group)
        ImageView ivCarGroup;
        @BindView(R.id.tv_car_group_title)
        TextView tvCarGroupTitle;

        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_car_item_xuanzhong)
        CheckBox ivCarItemXuanzhong;
        @BindView(R.id.iv_car_goods)
        ImageView ivCarGoods;
        @BindView(R.id.tv_car_group_title)
        TextView tvCarGroupTitle;
        @BindView(R.id.tv_car_grout_price)
        TextView tvCarGroutPrice;
        @BindView(R.id.iv_car_jian)
        ImageView ivCarJian;
        @BindView(R.id.tv_car_group_count)
        TextView tvCarGroupCount;
        @BindView(R.id.iv_car_jia)
        ImageView ivCarJia;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface PriceChange {
        void change();
    }

}
